{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d472f1ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路线规划汇总结果已保存到 '中诚大厦路线规划.xlsx'\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "\n",
    "# 1. 读取Excel文件\n",
    "excel_path = '中诚大厦周边数据表格.xlsx'  # Excel文件路径\n",
    "all_coords = []\n",
    "\n",
    "# 2. 提取所有坐标\n",
    "for sheet_name in pd.read_excel(excel_path, sheet_name=None):  # 读取所有工作表\n",
    "    coords = pd.read_excel(excel_path, sheet_name=sheet_name)['location'].tolist()  # 假设列名为'Coordinates'\n",
    "    all_coords.extend(coords)\n",
    "\n",
    "# 3. 设置起点坐标\n",
    "start_point = '114.277158,30.592578'\n",
    "\n",
    "# 4. 使用路线规划API\n",
    "api_key = 'eca5e60c4f1ef3764871a52497c013ec'  # 替换为你的高德地图API密钥\n",
    "base_url = \"https://restapi.amap.com/v3/direction/driving\"\n",
    "\n",
    "# 初始化结果列表\n",
    "route_results = []\n",
    "\n",
    "# 5. 发送请求并保存结果\n",
    "for coord in all_coords:\n",
    "    # 对坐标进行URL编码\n",
    "    coord_encoded = requests.utils.quote(coord)\n",
    "    start_point_encoded = requests.utils.quote(start_point)\n",
    "    url = f\"{base_url}?origin={start_point_encoded}&destination={coord_encoded}&key={api_key}\"\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        route_results.append(response.json())\n",
    "    else:\n",
    "        print(f\"请求失败，状态码：{response.status_code}\")\n",
    "\n",
    "# 6. 汇总结果\n",
    "# 假设我们要保存的列名为 '起点', '终点', '路线距离', '路线时间'\n",
    "output_df = pd.DataFrame({\n",
    "    '起点': [start_point] * len(all_coords),  # 起点相同，所以重复起点坐标\n",
    "    '终点': all_coords,  # Excel中所有地点的坐标\n",
    "    # 假设结果中包含distance和duration，根据实际API响应调整\n",
    "    '路线距离': [r['route']['paths'][0]['distance'] for r in route_results if 'route' in r],\n",
    "    '路线时间': [r['route']['paths'][0]['duration'] for r in route_results if 'route' in r],\n",
    "})\n",
    "\n",
    "# 7. 保存汇总结果到Excel\n",
    "output_excel_path = '中诚大厦路线规划.xlsx'\n",
    "output_df.to_excel(output_excel_path, index=False)\n",
    "print(f\"路线规划汇总结果已保存到 '{output_excel_path}'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7486f0da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路线规划汇总结果已保存到 '良友大厦路线规划.xlsx'\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "\n",
    "# 1. 读取Excel文件\n",
    "excel_path = '良友大厦周边数据表格.xlsx'  # Excel文件路径\n",
    "all_coords = []\n",
    "\n",
    "# 2. 提取所有坐标\n",
    "for sheet_name in pd.read_excel(excel_path, sheet_name=None):  # 读取所有工作表\n",
    "    coords = pd.read_excel(excel_path, sheet_name=sheet_name)['location'].tolist()  # 假设列名为'Coordinates'\n",
    "    all_coords.extend(coords)\n",
    "\n",
    "# 3. 设置起点坐标\n",
    "start_point = '114.273801,30.59283'\n",
    "\n",
    "# 4. 使用路线规划API\n",
    "api_key = 'eca5e60c4f1ef3764871a52497c013ec'  # 替换为你的高德地图API密钥\n",
    "base_url = \"https://restapi.amap.com/v3/direction/driving\"\n",
    "\n",
    "# 初始化结果列表\n",
    "route_results = []\n",
    "\n",
    "# 5. 发送请求并保存结果\n",
    "for coord in all_coords:\n",
    "    # 对坐标进行URL编码\n",
    "    coord_encoded = requests.utils.quote(coord)\n",
    "    start_point_encoded = requests.utils.quote(start_point)\n",
    "    url = f\"{base_url}?origin={start_point_encoded}&destination={coord_encoded}&key={api_key}\"\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        route_results.append(response.json())\n",
    "    else:\n",
    "        print(f\"请求失败，状态码：{response.status_code}\")\n",
    "\n",
    "# 6. 汇总结果\n",
    "# 假设我们要保存的列名为 '起点', '终点', '路线距离', '路线时间'\n",
    "output_df = pd.DataFrame({\n",
    "    '起点': [start_point] * len(all_coords),  # 起点相同，所以重复起点坐标\n",
    "    '终点': all_coords,  # Excel中所有地点的坐标\n",
    "    # 假设结果中包含distance和duration，根据实际API响应调整\n",
    "    '路线距离': [r['route']['paths'][0]['distance'] for r in route_results if 'route' in r],\n",
    "    '路线时间': [r['route']['paths'][0]['duration'] for r in route_results if 'route' in r],\n",
    "})\n",
    "\n",
    "# 7. 保存汇总结果到Excel\n",
    "output_excel_path = '良友大厦路线规划.xlsx'\n",
    "output_df.to_excel(output_excel_path, index=False)\n",
    "print(f\"路线规划汇总结果已保存到 '{output_excel_path}'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "62dcd664",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路线规划汇总结果已保存到 '武银大厦路线规划.xlsx'\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "\n",
    "# 1. 读取Excel文件\n",
    "excel_path = '武银大厦周边数据表格.xlsx'  # Excel文件路径\n",
    "all_coords = []\n",
    "\n",
    "# 2. 提取所有坐标\n",
    "for sheet_name in pd.read_excel(excel_path, sheet_name=None):  # 读取所有工作表\n",
    "    coords = pd.read_excel(excel_path, sheet_name=sheet_name)['location'].tolist()  # 假设列名为'Coordinates'\n",
    "    all_coords.extend(coords)\n",
    "\n",
    "# 3. 设置起点坐标\n",
    "start_point = '114.273599,30.596733'\n",
    "\n",
    "# 4. 使用路线规划API\n",
    "api_key = 'eca5e60c4f1ef3764871a52497c013ec'  # 替换为你的高德地图API密钥\n",
    "base_url = \"https://restapi.amap.com/v3/direction/driving\"\n",
    "\n",
    "# 初始化结果列表\n",
    "route_results = []\n",
    "\n",
    "# 5. 发送请求并保存结果\n",
    "for coord in all_coords:\n",
    "    # 对坐标进行URL编码\n",
    "    coord_encoded = requests.utils.quote(coord)\n",
    "    start_point_encoded = requests.utils.quote(start_point)\n",
    "    url = f\"{base_url}?origin={start_point_encoded}&destination={coord_encoded}&key={api_key}\"\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        route_results.append(response.json())\n",
    "    else:\n",
    "        print(f\"请求失败，状态码：{response.status_code}\")\n",
    "\n",
    "# 6. 汇总结果\n",
    "# 假设我们要保存的列名为 '起点', '终点', '路线距离', '路线时间'\n",
    "output_df = pd.DataFrame({\n",
    "    '起点': [start_point] * len(all_coords),  # 起点相同，所以重复起点坐标\n",
    "    '终点': all_coords,  # Excel中所有地点的坐标\n",
    "    # 假设结果中包含distance和duration，根据实际API响应调整\n",
    "    '路线距离': [r['route']['paths'][0]['distance'] for r in route_results if 'route' in r],\n",
    "    '路线时间': [r['route']['paths'][0]['duration'] for r in route_results if 'route' in r],\n",
    "})\n",
    "\n",
    "# 7. 保存汇总结果到Excel\n",
    "output_excel_path = '武银大厦路线规划.xlsx'\n",
    "output_df.to_excel(output_excel_path, index=False)\n",
    "print(f\"路线规划汇总结果已保存到 '{output_excel_path}'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa008529",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
